package com.ljj.examisto.ui.dialog

import android.app.Activity
import android.content.Context
import android.content.ContextWrapper
import android.content.Intent
import android.view.Gravity
import android.view.LayoutInflater
import android.view.WindowManager
import androidx.appcompat.app.AlertDialog
import com.ljj.examisto.R
import com.ljj.examisto.ui.base.BaseDialog
import com.ljj.examisto.data.service.TimeService
import com.ljj.examisto.databinding.DialogMenuBinding
import com.ljj.examisto.ui.activity.SettingActivity

class MenuDialog (context: Context) : BaseDialog(
    context, R.style.LeftSlideDialog
) {
    private val binding: DialogMenuBinding by lazy {
        DialogMenuBinding.inflate(LayoutInflater.from(context))
    }

    init {
        // 基础设置
        setContentView(binding.root)
        setCanceledOnTouchOutside(true)
        setupViews()
        // 窗口属性设置
        window?.apply {
            setLayout(
                (context.resources.displayMetrics.widthPixels * 0.7).toInt(),
                WindowManager.LayoutParams.MATCH_PARENT
            )
            setGravity(Gravity.START)
        }
    }

    /* 流程函数 */
    private fun setupViews() {
        setupFullQuitButton()
        setupTipsButton()
        setupSettingButton()
    }

    /* 工具函数 */
    private fun setupFullQuitButton() {
        binding.btnFullQuit.setOnClickListener {
            AlertDialog.Builder(context)
                .setTitle("是否退出")
                .setMessage("是否停止前台服务以完全退出应用？")
                .setPositiveButton("确认") { _, _ ->
                    endAll()
                }
                .setNegativeButton("取消", null)
                .show()
        }
    }

    private fun setupTipsButton() {
        binding.btnTips.setOnClickListener {
            AlertDialog.Builder(context)
                .setTitle("建议调整的设置选项")
                .setMessage(
                    "①设置→应用→应用启动管理→${context.getString(R.string.app_name)}→手动管理→允许自启动，允许后台活动" + "\n" +
                    "②设置→应用→应用管理→${context.getString(R.string.app_name)}→通知管理→${context.getString(R.string.notification_channel_name)}→允许通知打开，静默通知关闭，锁屏通知和横幅通知关闭，通知铃声静音，允许打扰打开" + "\n" +
                    "③设置→应用→应用管理→${context.getString(R.string.app_name)}→通知管理→${context.getString(R.string.remind_channel_name)}→允许通知打开，静默通知关闭，锁屏通知和横幅通知开启，允许打扰打开" + "\n"
                )
                .setPositiveButton("了解") { dialog, _ ->
                    dialog.dismiss()
                }
                .show()
        }
    }

    private fun setupSettingButton() {
        binding.btnSetting.setOnClickListener {
            dismiss()
            context.startActivity(Intent(context, SettingActivity::class.java))
        }
    }

    private fun endAll() {
        dismiss()                                   // 关闭HomeActivity上的SettingDialog实例
        TimeService.stopService(context)            // 停止前台服务
        getActivityContext()?.finishAffinity()      // 关闭和HomeActivity同一个任务栈的所有Activity
    }

    private fun getActivityContext(): Activity? {
        var ctx = context
        while (ctx is ContextWrapper) {
            if (ctx is Activity) {
                return ctx
            }
            ctx = ctx.baseContext
        }
        return null
    }
}